<?php

namespace App\Http\Controllers\Wap;

use App\Models\Cates;
use App\Models\Configs;
use App\Models\News;
use App\Models\Praises;
use App\Models\Tags;
use App\Models\Users;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    protected $chanel_id = 3;//设置导航选中标志

    public function __construct()
    {
        view()->share('chanel_id',$this->chanel_id);
    }


    public function index()
    {
        $pageTitle = '';
        return view('wap.user.index',compact('pageTitle'));
    }

    public function detail(Request $request){
        if($request->method() == 'POST'){
            $data = $request->all();
            auth()->user()->update($data);
            return $this->writeJson(0);
        }else{
            $left_id = 1;//设置用户左边选中
            return view('wap.user.detail',compact('left_id'));
        }
    }

    public function password(Request $request)
    {
        if($request->method() == 'POST'){
            $data = request()->all();
            $rules = [
                'password_old' => 'required',
                'password'  => 'required|min:6|confirmed',
            ];
            $msgs = [
                'password_old.required' => '请输入旧密码',
                'password.required' => '请输入新密码',
                'password.min'      => '密码最少6位',
                'password.confirmed'=> '两次输入密码不一致',
            ];
            $validator = validator()->make($data,$rules,$msgs);
            if ($validator->fails()) {
                return $this->writeJson(1,null,$validator->errors()->first());
            }
            if((\Hash::check($data['password_old'], auth()->user()->password)) === false){
                return $this->writeJson(1,null,'旧密码输入错误');
            }
            if($data['password_old'] == $data['password']){
                return $this->writeJson(1,null,'新密码和旧密码一致');
            }
            auth()->user()->update(['password'=>bcrypt($data['password'])]);
            return $this->writeJson(0);
        }else{
            $left_id = 4;//设置用户左边选中
            return view('wap.user.password',compact('left_id'));
        }
    }

    public function join(Request $request){
        if($request->method() == 'POST'){
            $data = request()->all();
            $rules = [
                'mobile'   => 'required',
                'truename' => 'required',
                'card'     => 'required',
                'material' => 'required',
                'protocol' => 'accepted'
            ];
            $msgs = [
                'truename.required' => '请输入真实姓名',
                'card.required'     => '请输入身份证号码',
                'mobile.required'   => '请填写手机号',
                'material.required' => '请上传投稿材料',
                'protocol.accepted' => '您还未阅读并同意《免责申明》',
            ];
            $validator = validator()->make($data,$rules,$msgs);
            if ($validator->fails()) {
                return $this->writeJson(1,null,$validator->errors()->first());
            }
            $data['column'] = Users::COLUMN_1;
            $data['apply_at'] = Carbon::now();
            auth()->user()->update($data);
            return $this->writeJson(0);
        }else{
            return view('wap.user.join');
        }
    }

    public function publish(Request $request){
        if($request->method() == 'POST'){
            $data = request()->all();
            $rules = [
                'title'  => 'required',
                'summary'=> 'required',
                'thumb'  => 'required',
                'cateid' => 'required',
                'tags'   => 'required',
                'content'=> 'required',

            ];
            $msgs = [
                'title.required'  => '请填写标题',
                'summary.required'=> '请填写摘要',
                'cateid.required' => '请选择类目',
                'tags.required'   => '请选择标签',
                'thumb.required'  => '请上传封面图',
                'content.required'=> '请填写内容',
            ];
            $validator = validator()->make($data,$rules,$msgs);
            if ($validator->fails()) {
                return $this->writeJson(1,null,$validator->errors()->first());
            }
            if(!is_numeric($data['cateid'])){
                $data['cateid'] = explode('-',$data['cateid']);
                $data['catepid'] = $data['cateid'][0];
                $data['cateid'] = $data['cateid'][1];
            }else{
                $data['catepid'] = $data['cateid'][0];
                $data['cateid'] = 0;
            }
            $data['author_id'] = auth()->user()->id;
            if(isset($data['id']) && $data['id']){
                $info = News::findOrFail($data['id']);
                if($info['status'] == News::STATUS_3) $data['status'] = News::STATUS_2;
                $info->update($data);
                return $this->writeJson(0,null,'修改文章信息成功');
            }else{
                unset($data['id']);
                $data['status'] = News::STATUS_2;
                News::create($data);
                $setting = Configs::info('integral');
                auth()->user()->increment('integral',$setting['publish']);
                return $this->writeJson(0,null,'发布文章信息成功');
            }
        }else{
            $left_id = 2;//设置用户左边选中
            //查询新闻类目
            $categories = json_encode(Cates::wapCategories(1000));
            //查询新闻标签
            $labels = json_encode(Tags::tags());
            return view('wap.user.publish',compact('left_id','labels','categories'));
        }
    }

    public function edit(Request $request){
        $left_id = 2;//设置用户左边选中
        $id = (int)$request->get('id',0);
        $fields = ['id','title','summary','thumb','cateid','catepid','tags','content'];
        $params = [
            'author_id' => auth()->user()->id
        ];
        $info = News::findOne($id,$fields,$params);
        if(count($info['cateid']) > 1){
            $info['cateid'] = implode('-',$info['cateid']);
        }else{
            $info['cateid'] = $info['cateid'][0];
        }
        $info = json_encode($info);
        //查询新闻类目
        $categories = json_encode(Cates::wapCategories(1000));
        //查询新闻标签
        $labels = json_encode(Tags::tags());
        return view('wap.user.edit',compact('left_id','labels','categories','info'));
    }

    public function article(Request $request)
    {
        $left_id = 2;//设置用户左边选中
        //获取列表
        $params = [
            'author_id'  => auth()->user()->id,
        ];
        $fields = ['id','title','summary','view','created_at','thumb','cateid','author_id','status','catepid','praise'];
        $lists = News::home($fields,$params,10);
        if($request->ajax()){
            return $this->writeJson(0,$lists);
        }
        $lists = json_encode($lists);

        return view('wap.user.article',compact('left_id','lists'));
    }

    public function destroy(Request $request)
    {
        $id = (int)$request->get('id',0);
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        News::destroy($id);
        return $this->writeJson(0);
    }


    public function praise(Request $request)
    {
        $left_id = 3;//设置用户左边选中
        $params = [
            'author_id'  => auth()->user()->id,
        ];
        $lists = Praises::list($params,10);
        if($request->ajax()){
            return $this->writeJson(0,$lists);
        }
        $lists = json_encode($lists);
        return view('wap.user.praise',compact('left_id','lists'));
    }

}